package com.ntschy.taizhou.controller;


import com.ntschy.taizhou.config.AjaxResult;
import com.ntschy.taizhou.config.StringUtils;
import com.ntschy.taizhou.entity.StoreOrderCensusDto;
import com.ntschy.taizhou.service.HomeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/storeOrderCensus")
/* 类注解 */
@Api(value = "数据展示")
public class StoreOrderCensusController {

    @Autowired
    HomeService homeService;

    @GetMapping("/getBrandName")
    @ApiOperation(value = "获取BrandName 列表", notes = "")
    public AjaxResult getBrandName() {

        List<String> list = homeService.getBrandName();
        return AjaxResult.success(list);

    }

    @GetMapping("/getBusinessSegment")
    @ApiOperation(value = "获取BusinessSegment列表", notes = "")
    public AjaxResult getBusinessSegment() {

        List<String> list = homeService.getBusinessSegment();
        return AjaxResult.success(list);

    }

    @GetMapping("/getSector")
    @ApiOperation(value = "获取Sector 列表", notes = "")
    public AjaxResult getSector() {

        List<String> list = homeService.getSector();
        return AjaxResult.success(list);

    }

    @GetMapping("/getCategory")
    @ApiOperation(value = "获取 Category 列表", notes = "")
    public AjaxResult getCategory() {

        List<String> list = homeService.getCategory();
        return AjaxResult.success(list);

    }

    @GetMapping("/getSalesDestination")
    @ApiOperation(value = "获取  sales_destination 列表", notes = "")
    public AjaxResult getSalesDestination() {

        List<String> list = homeService.getSalesDestination();
        return AjaxResult.success(list);

    }

    @GetMapping("/getAllCensus")
    @ApiOperation(value = "  B 区的 两个数据 ", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 2021-08", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 2021-09", dataType = "String")
    })
    public AjaxResult getAllCensus(String startTime, String endTime) {
        if (StringUtils.isEmpty(startTime)) {
            startTime = homeService.getMinTime();
        }
        if (StringUtils.isEmpty(endTime)) {
            endTime = homeService.getMaxTime();
        }
        StoreOrderCensusDto map = homeService.getAllCensus(startTime, endTime);

        return AjaxResult.success(map);

    }


    @GetMapping("/getGmvArea")
    @ApiOperation(value = "  C 区的 数据 ", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 2021-08", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 2021-09", dataType = "String"),
            @ApiImplicitParam(name = "brandName", value = "brandName", dataType = "String"),
            @ApiImplicitParam(name = "businessSegment", value = "businessSegment", dataType = "String"),
            @ApiImplicitParam(name = "sector", value = "sector", dataType = "String"),
            @ApiImplicitParam(name = "category", value = "category", dataType = "String"),
            @ApiImplicitParam(name = "salesDestination", value = "salesDestination", dataType = "String"),
    })
    public AjaxResult getGmvArea(String startTime, String endTime, String brandName
            , String businessSegment, String sector, String category, String salesDestination) {
        if (StringUtils.isEmpty(startTime)) {
            startTime = homeService.getMinTime();
        }
        if (StringUtils.isEmpty(endTime)) {
            endTime = homeService.getMaxTime();
        }
        startTime=startTime+"-01";
        endTime=endTime+"-01";
        List<Map<String,Object>> maps=new ArrayList<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, df);
        LocalDate endTDate = LocalDate.parse(endTime, df);
        while (startDate.isBefore(endTDate.plusMonths(1)) ) {
            DateTimeFormatter dfm = DateTimeFormatter.ofPattern("yyyy-MM");
          String time=  dfm.format(startDate);
            // 逻辑处理
            Map<String,Object> map= homeService.getGmvArea(time,brandName,businessSegment,sector,category,salesDestination);
            maps.add(map);
            startDate=  startDate.plusMonths(1);
        }

        return AjaxResult.success(maps);

    }


    @GetMapping("/getRevenueArea")
    @ApiOperation(value = "  D 区的 两个数据 ", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 2021-08", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 2021-09", dataType = "String"),
            @ApiImplicitParam(name = "brandName", value = "brandName", dataType = "String"),
            @ApiImplicitParam(name = "businessSegment", value = "businessSegment", dataType = "String"),
            @ApiImplicitParam(name = "sector", value = "sector", dataType = "String"),
            @ApiImplicitParam(name = "category", value = "category", dataType = "String"),
            @ApiImplicitParam(name = "salesDestination", value = "salesDestination", dataType = "String"),
    })
    public AjaxResult getRevenueArea(String startTime, String endTime, String brandName
            , String businessSegment, String sector, String category, String salesDestination) {

        if (StringUtils.isEmpty(startTime)) {
            startTime = homeService.getMinTime();
        }
        if (StringUtils.isEmpty(endTime)) {
            endTime = homeService.getMaxTime();
        }
        startTime=startTime+"-01";
        endTime=endTime+"-01";
        List<Map<String,Object>> maps=new ArrayList<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, df);
        LocalDate endTDate = LocalDate.parse(endTime, df);
        while (startDate.isBefore(endTDate.plusMonths(1)) ) {
            DateTimeFormatter dfm = DateTimeFormatter.ofPattern("yyyy-MM");
            String time=  dfm.format(startDate);
            // 逻辑处理
            Map<String,Object> map= homeService.getRevenueArea(time,brandName,businessSegment,sector,category,salesDestination);
            maps.add(map);
            startDate=  startDate.plusMonths(1);
        }

        return AjaxResult.success(maps);

    }

//    @ApiOperation(value = "获取BrandName 列表", notes = "")
//    public Object queryAll( /* 参数注解 */ @ApiParam(value = "测试参数名称", required = true) @RequestParam String name) {
//        System.out.println(name);
//        List<TestUser> list = helloListDao.queryAll();
//
//        return list;
//
//    }


}
